using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using common = Microsoft.ConnectionEngine.Common;
using Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Common;
using business = Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Business;

using Microsoft.ConnectionEngine.Common.Security;


namespace Microsoft.ConnectionEngine.Registers.Core.ServiceProvider.Facade
{
    class ServiceProviderTypeManager
    {
        private ServiceProviderTypeManager() { }

        //ServiceProviderTypeSearchRequest   	 1041		
			public static void ServiceProviderTypeSearch(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
                ServiceProviderTypes typeList = new ServiceProviderTypes();

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

                ServiceProviderTypeSearchRequest request = new ServiceProviderTypeSearchRequest();
                XmlNode searchNode = xmlMessage.SelectSingleNode("//pnp:ServiceProviderTypeSearchRequest", nsmgr);
                request = (ServiceProviderTypeSearchRequest)common.Utilities.DeserializeObject(searchNode.OuterXml, typeof(ServiceProviderTypeSearchRequest));

                typeList = business.ServiceProviderTypeManager.ServiceProviderTypeSearch(request);
							
				resultMsgType = (int)MessageTypes.ServiceProviderTypeSearchResult;
				resultXmlMessage = new XmlDocument();
                resultXmlMessage.LoadXml(common.Utilities.SerializeObject(typeList, typeof(ServiceProviderTypes)));
			}


            //ServiceProviderTypeSelectRequest 1064
			public static void ServiceProviderTypeSelectItem(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
				ServiceProviderType serviceProviderType = new ServiceProviderType();

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				string serviceProviderTypeID = xmlMessage.SelectSingleNode("//pnp:ServiceProviderTypeID", nsmgr).InnerText;
                serviceProviderType = business.ServiceProviderTypeManager.ServiceProviderTypeSelectItem(serviceProviderTypeID);               				
			
				resultMsgType = (int)MessageTypes.ServiceProviderTypeSelectResult;
				resultXmlMessage = new XmlDocument();
                resultXmlMessage.LoadXml(common.Utilities.SerializeObject(serviceProviderType, typeof(ServiceProviderType)));
			}

            //ServiceProviderTypeCheckIn 1031
			public static void ServiceProviderTypeUnlock(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
                nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

                XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
                HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
                
                String serviceProviderTypeID = xmlMessage.SelectSingleNode("//pnp:ServiceProviderTypeID", nsmgr).InnerText;
                //String status = business.ServiceProviderTypeManager.ServiceProviderTypeUnlock(serviceProviderTypeID, currentUser.HSDIdentity.UserId);
                business.ServiceProviderTypeManager.ServiceProviderTypeUnlock(serviceProviderTypeID, currentUser.HSDIdentity.UserId);

				resultMsgType = 0;
				resultXmlMessage = new XmlDocument();				
			}

            //ServiceProviderTypeCheckInAll 1039
			public static void ServiceProviderTypeUnlockAll(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
            {
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
                nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

                XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
                HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
                
                //String status = business.ServiceProviderTypeManager.ServiceProviderUnlockAll(currentUser.HSDIdentity.UserId);
                business.ServiceProviderTypeManager.ServiceProviderUnlockAll(currentUser.HSDIdentity.UserId);  
										
				resultMsgType = 0;
				resultXmlMessage = new XmlDocument();				
			}

            //ServiceProviderTypeCheckOut 1032
			public static void ServiceProviderTypeLock(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
				ServiceProviderType serviceProviderType = new ServiceProviderType();

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
                
				String serviceProviderTypeID = xmlMessage.SelectSingleNode("//pnp:ServiceProviderTypeID", nsmgr).InnerText;
		        //String status = business.ServiceProviderTypeManager.ServiceProviderTypeLock(serviceProviderTypeID, currentUser.HSDIdentity.UserId);
                business.ServiceProviderTypeManager.ServiceProviderTypeLock(serviceProviderTypeID, currentUser.HSDIdentity.UserId);

                serviceProviderType = business.ServiceProviderTypeManager.ServiceProviderTypeSelectItem(serviceProviderTypeID);               

				resultMsgType = (int)MessageTypes.ServiceProviderTypeCheckOutResult;
				resultXmlMessage = new XmlDocument();
				resultXmlMessage.LoadXml(common.Utilities.SerializeObject(serviceProviderType, typeof(ServiceProviderType)));
			}

            //ServiceProviderTypeInsert 1034
			public static void ServiceProviderTypeInsert(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
                ServiceProviderType serviceProviderType = new ServiceProviderType();

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
                
				XmlNode serviceProviderTypeNode = xmlMessage.SelectSingleNode("//pnp:ServiceProviderType", nsmgr);
                serviceProviderType = (ServiceProviderType)common.Utilities.DeserializeObject(serviceProviderTypeNode.OuterXml, typeof(ServiceProviderType));
                //String status = business.ServiceProviderTypeManager.ServiceProviderTypeInsert(serviceProviderType, currentUser.HSDIdentity.UserId);
                business.ServiceProviderTypeManager.ServiceProviderTypeInsert(serviceProviderType, currentUser.HSDIdentity.UserId);

                serviceProviderType = business.ServiceProviderTypeManager.ServiceProviderTypeSelectItem(serviceProviderType.ServiceProviderTypeID.ToString());

				resultMsgType = (int)MessageTypes.ServiceProviderTypeInsertResult;
				resultXmlMessage = new XmlDocument();
                resultXmlMessage.LoadXml(common.Utilities.SerializeObject(serviceProviderType, typeof(ServiceProviderType)));
			}

            //ServiceProviderTypeUpdate 1033
			public static void ServiceProviderTypeUpdate(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
				ServiceProviderType serviceProviderType = new ServiceProviderType();

				XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
				nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

				XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
				HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);   
                
				XmlNode serviceProviderTypeNode = xmlMessage.SelectSingleNode("//pnp:ServiceProviderType", nsmgr);
                serviceProviderType = (ServiceProviderType)common.Utilities.DeserializeObject(serviceProviderTypeNode.OuterXml, typeof(ServiceProviderType));
                //String status = business.ServiceProviderTypeManager.ServiceProviderTypeUpdate(serviceProviderType, currentUser.HSDIdentity.UserId);
                business.ServiceProviderTypeManager.ServiceProviderTypeUpdate(serviceProviderType, currentUser.HSDIdentity.UserId);

                serviceProviderType = business.ServiceProviderTypeManager.ServiceProviderTypeSelectItem(serviceProviderType.ServiceProviderTypeID.ToString());

				resultMsgType = (int)MessageTypes.ServiceProviderTypeUpdateResult;
				resultXmlMessage = new XmlDocument();
                resultXmlMessage.LoadXml(common.Utilities.SerializeObject(serviceProviderType, typeof(ServiceProviderType)));
			}

            //ServiceProviderTypeDelete 1030
			public static void ServiceProviderTypeDelete(XmlDocument xmlMessage, out XmlDocument resultXmlMessage, out int resultMsgType)
			{
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlMessage.NameTable);
                nsmgr.AddNamespace("pnp", xmlMessage.FirstChild.NamespaceURI);

                XmlNode userCredentials = xmlMessage.SelectSingleNode("//pnp:UserCredentials", nsmgr);
                HSDPrincipal currentUser = Utilities.CreateUser(userCredentials, nsmgr);
                
                String serviceProviderTypeID = xmlMessage.SelectSingleNode("//pnp:ServiceProviderTypeID", nsmgr).InnerText;
                //String status = business.ServiceProviderTypeManager.ServiceProviderTypeDelete(serviceProviderTypeID, currentUser.HSDIdentity.UserId);
                business.ServiceProviderTypeManager.ServiceProviderTypeDelete(serviceProviderTypeID, currentUser.HSDIdentity.UserId); 
			
				resultMsgType = 0;
				resultXmlMessage = new XmlDocument();				
			}
			
			
			
    }
}
